﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Domain.Entities;
using System.Domain.IRepository;

namespace Wk.System.Domain.Services
{
    public class MenuService
    {
        private readonly IMenuRepository _menuRepository;
        public MenuService(IMenuRepository menuRepository) 
        {
            _menuRepository = menuRepository;
        }
        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <returns></returns>
        public async Task<Menu> GetOneMenuAsync(int id) 
        {
            return await _menuRepository.GetEntityAsync(id);
        }
        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>
        public async Task<List<Menu>> GetAllMenusAsync() 
        {
            return await _menuRepository.GetAllMenusAsync();
        }
        /// <summary>
        /// 获取菜单树形
        /// </summary>
        /// <returns></returns>
        public async Task<List<Menu>> GetMenuTreeAsync(int roleId,int isOutStaff=0)
        {
            var list= await _menuRepository.GetMenuTreeAsync(roleId);
            if (roleId==4&& isOutStaff == 1)
            {
                RemoveNode(list);
            }
            return list;
        }
        /// <summary>
        /// 移除不需要的菜单
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="name"></param>
        /// <param name="data"></param>
        public void RemoveNode(List<Menu> nodes)
        {
            for (int i = nodes.Count-1; i >=0; i--)
            {
                if (nodes[i].name.Contains("打分"))
                {
                    nodes.RemoveAt(i);
                    continue;
                }
                if (nodes[i].child != null)
                {
                    RemoveNode(nodes[i].child);
                }
            }
          
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <returns></returns>
        public async Task<bool> AddMenuAsync(Menu menu) 
        {
            return await _menuRepository.InsertAsync(menu);
        }
        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <returns></returns>
        public async Task<bool> UpdateMenuAsync(Menu menu)
        {
            return await _menuRepository.UpdateAsync(menu);
        }
        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <returns></returns>
        public async Task<bool> DeleteMenuAsync(int id)
        {
            var list = new List<int>();
            list.Add(id);
            await FindChildId(id, list);
            return await _menuRepository.DeleteAsync(list);
        }
        public async Task FindChildId(int menuId,List<int> ids)
        {
            var childList = await _menuRepository.GetChildListAsync(menuId);
            foreach (var node in childList)
            {
                ids.Add(node.id);
                await FindChildId(node.id, ids);
            }
        }
        public async Task<List<Menu>> GetButtonlist(string url,int typeId,int roleId) 
        {
            return await _menuRepository.GetButtonlist(url, typeId, roleId);
        }
    }
}
